package com.tosit.driverschool.service;

import com.tosit.driverschool.common.entity.BelongTo;
import com.tosit.driverschool.common.entity.ProblemSet;
import com.tosit.driverschool.common.entity.SetType;

import com.tosit.driverschool.common.entity.Subject;
import com.tosit.driverschool.dao.BelongToMapper;
import com.tosit.driverschool.dao.ProblemSetMapper;
import com.tosit.driverschool.dao.SetTypeRepository;
import com.tosit.driverschool.dao.SubjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ProblemSetServiceImpl implements ProblemSetService{

    @Autowired
    ProblemSetMapper problemSetMapper;
    @Autowired
    SetTypeRepository setTypeRepository;
    @Autowired
    BelongToMapper belongToMapper;
    @Autowired
    SubjectRepository subjectRepository;

    @Override
    public Set<SetType> findAllType(String subject, String vehicleType) {
        List<SetType> all = setTypeRepository.findAll();
        Set<SetType> types = new HashSet<>();
        for (SetType setType : all) {
            if(setType.getSubject().equals(subject) && setType.getVehicleType().equals(vehicleType)) {
                types.add(setType);
            }
        }
        return types;
    }

    @Override
    public List<ProblemSet> findAll(Integer typeId) {
        List<ProblemSet> all = problemSetMapper.findAll(typeId);
        return all;
    }

    @Override
    public Integer delete(Integer id) {
        Integer result = problemSetMapper.deleteByPrimaryKey(id);
        return result;
    }

    public List<Subject> find(Integer setId){
        List<BelongTo> all = belongToMapper.findAll(setId);
        List<Integer> ids = new ArrayList<>();
        for(BelongTo belongTo : all){
            ids.add(belongTo.getSubjectId());
        }
        for(Integer id : ids){
            System.out.println(id);
        }
        List<Subject> subjects = subjectRepository.findAllById(ids);
        return subjects;
    }

    @Override
    public void add(ProblemSet problemSet) {
        problemSetMapper.insert(problemSet);
    }

}
